// 1000 => 1,000
export const formatNumber = (number: number) => {
  if (isNaN(number)) {
    return number;
  }
  const symbol = number >= 0 ? '' : '-';
  const numStr = number.toString();
  const numArr = numStr.match(/(\d+(?:\.\d{1,2})?)\d*/) || [];
  return symbol + numArr[1].replace(/(\d)(?=(?:\d{3})+(?:\.\d{1,2})?$)/g, '$1,');
};

export const getDay = (day: any) => {
  const today = new Date();
  const targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
  today.setTime(targetday_milliseconds);
  const tYear = today.getFullYear();
  let tMonth = today.getMonth();
  let tDate = today.getDate();
  tMonth = doHandleMonth(tMonth + 1);
  tDate = doHandleMonth(tDate);
  return tYear + '-' + tMonth + '-' + tDate;
};

export const doHandleMonth = (month: any) => {
  let m = month;
  if (month.toString().length == 1) {
    m = '0' + month;
  }
  return m;
};

export const dataTimeRangeCurTwo = (day = -1) => {
  const today = getDay(0);
  const yesToday = getDay(day);
  const end = `${today} 23:59:59`;
  const start = `${yesToday} 00:00:00`;
  return [Math.floor(+new Date(start) / 1000), Math.floor(+new Date(end) / 1000)];
};

const timeZoneOffsetGet = () => {
  let jsOffset = new Date().getTimezoneOffset();
  // jsOffset /= -60;
  jsOffset /= 60;
  return jsOffset;
};

export const dateTransToSeoulTime = (date: any) => {
  let _time = date * 1000;
  if (typeof date !== 'number') {
    _time = +new Date(date);
  }

  const jsOffset = timeZoneOffsetGet(); // -8

  const oneHourMs = 3600000;
  const seoulTimeZoneOffset = -9;
  // const seoulTimeZoneOffset = -8

  const utc = _time + oneHourMs * seoulTimeZoneOffset;

  const myTimestamp = utc + oneHourMs * jsOffset * -1;

  return Math.floor(myTimestamp / 1000);
};

export const timestampToSeoul = (timeStr: any, format = 'Y-m-d H:i:s', offset = 9) => {
  const _time = new Date(timeStr).getTime();

  const oneHourMs = 3600000;
  const seoulZone = 9;
  let utc = _time;

  if (offset === 1) {
    const jsOffset = timeZoneOffsetGet(); // -8
    utc = _time + oneHourMs * jsOffset;
  }

  const transTime = utc + oneHourMs * seoulZone;

  return formatDate(new Date(transTime), format);
};

// 9 => 09
export const toTwo = (number: number) => {
  return number * 1 > 9 ? '' + number : '0' + number;
};

export const getTimeObj = (date: Date) => {
  return {
    Y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    H: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds()
  };
};

export const formatDate = (time: number | Date, format = 'Y-m-d H:i:s') => {
  if (typeof time === 'undefined') {
    return '';
  }
  if (typeof time !== 'object') {
    time = new Date(time);
  }
  const dateArr: {
    [key: string]: number;
  } = getTimeObj(time);

  const symbolMatch: RegExpMatchArray | null = format.match(/[/-](?=[md])/gi);
  const symbol = symbolMatch && symbolMatch.length ? symbolMatch[0] : '';

  let ret = '';
  const need: RegExpMatchArray | null = format.match(/([ymdhis])/gi);
  need &&
    need.map((item) => {
      let _item = toTwo(dateArr[item]);
      if ('Ymd'.includes(item)) {
        _item = symbol + _item;
      }
      if ('His'.includes(item)) {
        _item = ':' + _item;
      }
      ret += _item;
    });
  return ret.replace(symbol, '').replace(':', ' ');
};

export const isType = (object: any, typeStr: string) => {
  return Object.prototype.toString.call(object) === typeStr;
};

export const isArray = (object: any) => {
  return isType(object, '[object Array]');
};

export const isObject = (object: any) => {
  console.log(object);
  return isType(object, '[object Object]');
};

export const getTimeZone = () => {
  var offsetMinutes = new Date().getTimezoneOffset();
  const offsetHours = Math.abs(Math.floor(offsetMinutes / 60)); // 将偏差值转换为小时
  const offsetSign = offsetMinutes < 0 ? '+' : '-'; // 根据时区偏差的正负号确定东西半球
  var offsetMinutes = Math.abs(offsetMinutes % 60); // 获取分钟偏差

  return (
    'GMT' +
    offsetSign +
    offsetHours.toString().padStart(2, '0') +
    ':' +
    offsetMinutes.toString().padStart(2, '0')
  );
};
